package cn.sc.allinpay.allfavour.business.cashiertrans;

import android.os.Handler;
import android.text.TextUtils;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.base.db.DbDao;
import cn.sc.allinpay.allfavour.base.model.BaseModel;
import cn.sc.allinpay.allfavour.business.common.data.TransRecord;

/**
 * 交易流水
 * author：pany
 * on 2017/12/5 14:44
 */
public class CashierTransModel extends BaseModel {

    private Handler handler;

    public CashierTransModel() {
        this.handler = new Handler();
    }

    /**
     * 查询交易记录
     *
     * @param date      0：今日 1：所有
     * @param transType 0：全部 1：消费 2:撤销
     * @param orderNo   订单号
     * @param callBack
     */
    public void queryTransRecord(final int date, final int transType, final String orderNo, final TransCallBack callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<TransRecord> tempTransRecords = null;
                if (date == 1 && transType == 0) {//查询所有
                    DbDao<TransRecord> dbDao = getDao(TransRecord.class);
                    QueryBuilder<TransRecord, Integer> queryBuilder = dbDao.queryBuilder();
                    queryBuilder.orderBy("id", false);//倒叙
                    try {
                        if (!TextUtils.isEmpty(orderNo)) {
                            Where<TransRecord, Integer> where = queryBuilder.where();
                            where.like("ORDERID", "%"+orderNo+"%");
                            tempTransRecords = where.query();
                        }else {
                            tempTransRecords = queryBuilder.query();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                } else {
                    DbDao<TransRecord> dbDao = getDao(TransRecord.class);
                    QueryBuilder<TransRecord, Integer> queryBuilder = dbDao.queryBuilder();
                    queryBuilder.orderBy("TRANSTIME", false);
                    Where<TransRecord, Integer> where = queryBuilder.where();
                    try {
                        if (date == 0) {//今日
                            String crtDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                            where.eq("TRANSDATE", crtDate);
                        }
                        if (transType == 1) {//消费
                            where.and().eq("STATE", 1)
                                    .or().eq("STATE", 2)
                                    .or().eq("STATE", 3);
                        } else if (transType == 2) {//撤销
                            where.and().eq("STATE", 4).or().eq("STATE", 5);
                        }
                        if (!TextUtils.isEmpty(orderNo)) {
                            where.and().like("ORDERID", "%"+orderNo+"%");
                        }
                        tempTransRecords = where.query();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                final List<TransRecord> transRecords = tempTransRecords;
                //消费总金额:卡券+电子钱包+POS交易金额
                BigDecimal saleAmt = BigDecimal.ZERO;//消费金额
                if (transRecords != null && !transRecords.isEmpty()) {
                    for (int i = 0; i < transRecords.size(); i++) {
                        TransRecord record = transRecords.get(i);
                        //部分抵扣成功，全额抵扣成功
                        if (1 == record.getSellState() || 3 == record.getSellState()) {
                            if (!TextUtils.isEmpty(record.getDisCouponAmt())) {
                                saleAmt = saleAmt.add(new BigDecimal(record.getDisCouponAmt()));
                            }
                            if (!TextUtils.isEmpty(record.getDisEWalletAmt())) {
                                saleAmt = saleAmt.add(new BigDecimal(record.getDisEWalletAmt()));
                            }
                        }
                        //支付成功
                        if (2 == record.getState()) {
                            if (!TextUtils.isEmpty(record.getPaidAmt())) {
                                saleAmt = saleAmt.add(new BigDecimal(record.getPaidAmt()));
                            }
                        }
                    }
                }
                final String saleAmtStr = AmtUtils.formatY(saleAmt);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onResult(transRecords, transRecords != null ? transRecords.size() : 0, saleAmtStr);
                    }
                });
            }
        }).start();
    }


    public interface TransCallBack {

        /**
         * 交易记录
         *
         * @param resp 回调值
         */
        void onResult(List<TransRecord> resp, int size, String saleAmt);

    }
}
